package com.intexh.speedandroid.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.intexh.speedandroid.utils.LogUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import com.intexh.speedandroid.BuildConfig;
import com.intexh.speedandroid.pay.event.WechatPayEvent;

/**
 *
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {


    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new LinearLayout(WXPayEntryActivity.this));
        LogUtil.e("frank","微信支付初始化");
        api = WXAPIFactory.createWXAPI(this, BuildConfig.WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }


    /**
     * 支付回调方法，这个回调界面 所在的包名 必须是你再微信填写的包名+wxapi/WXPayEntryActivity.java
     * 不过不是这个包名，不会回调此界面
     */
    @Override
    public void onResp(BaseResp resp) {
        LogUtil.e("frank","微信支付完成：resp.getType()"+resp.getType()+" resp.errCode="+resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode==0){
                EventBus.getDefault().post(new WechatPayEvent("ok"));
            }else if(resp.errCode==-2){
                EventBus.getDefault().post(new WechatPayEvent("cancel"));
            }else{
                EventBus.getDefault().post(new WechatPayEvent("error"));
            }
        }else{
            EventBus.getDefault().post(new WechatPayEvent("error"));
        }
        finish();

    }

}